<?php
namespace app\common\model;
use think\Db;
class Order extends \think\Model 
{

    protected $name = 'order';
    protected $autoWriteTimestamp = true;
    use \traits\controller\Jump;
    public function verifyConsume($order, $info, $orderId,$shopConfig){
        $transStatus = false;
        $payeeOrder['payee_no']    = $shopConfig['vip_no'];
        $payeeOrder['pay_no']      = $info['vip_no'];
        $payeeOrder['order_id']    = $orderId;
        $payeeOrder['score']       = $order['score'];
        $payeeOrder['create_time'] = time();
        $payeeOrder['act_type']    = 1;
        
        Db::startTrans();
        try{
            Db::name('tb_vip_info')->where('vip_no',$info['vip_no'])->setDec('vip_score_y', $order['score']);
            //状态设置为1：已付款
            $this->where('order_id',$orderId)->setField('order_state', 1);
            //商家支付记录
            Db::name('payee_order') ->insert($payeeOrder);
            //更新商家消费数据代码金额
            Db::name('shop_config')->where('vip_no',$shopConfig['vip_no'])->setInc('vip_score_y', $order['score']);
            $transStatus = true;
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            \think\Log::record($e->getMessage());
            // 回滚事务
            Db::rollback();
        }
        return $transStatus;
    }
    public function refund($order, $info, $shopConfig, $score){
        $transStatus = false;
        $payeeOrder['payee_no']    = $shopConfig['vip_no'];
        $payeeOrder['pay_no']      = $info['vip_no'];
        $payeeOrder['order_id']    = $order['order_id'];
        $payeeOrder['score']       = $order['score'];
        $payeeOrder['create_time'] = time();
        $payeeOrder['act_type']    = 2;
        /*
        $order['unpack_refund'] = 0;
        $order['order_state']   = 2;
        $order['refund_score']  = $score;
        */
        Db::startTrans();
        try{
            Db::name('tb_vip_info')->where('vip_no',$info['vip_no'])->setInc('vip_score_y', $score);
            //状态设置为2：已退款
            $this->where('order_id',$order['order_id'])->setField('order_state', 2);
            //退部分商品
            //$this->allowField(['order_state','unpack_refund','refund_score'])->save($order, ['order_id' => $order['order_id']]);
            //商家支付记录
            Db::name('payee_order') ->insert($payeeOrder);
            //更新商家银消费数据代码金额
            Db::name('shop_config')->where('vip_no',$shopConfig['vip_no'])->setDec('vip_score_y',  $score);
            
            $transStatus = true;
            Db::commit();
        } catch (\Exception $e) {
            \think\Log::record($e->getMessage());
            // 回滚事务
            Db::rollback();
        }
        return $transStatus;
        
    }
    //消费数据代码充值
    public function deposit($deposit){
        $transStatus = false;
        Db::startTrans();
        try{
            Db::name('tb_vip_info')->where('vip_no',$deposit['vip_no'])->setInc('vip_score_y', $deposit['money']);
            Db::name('deposit') ->insert($deposit);
            $id = Db::name('deposit') ->getLastInsID();
            $data['id'] = $id;
            $transStatus = true;
            Db::commit();
        } catch (\Exception $e) {
            \think\Log::record($e->getMessage());
            // 回滚事务
            Db::rollback();
        }
        $data['transStatus'] = $transStatus;
        return $data;
    
    }
    
    //消费数据分配指数积分
    public function consumeToAddUserPoints($deposit){
        $domain = 'http://192.168.51.124:8080/';        
        $url= $domain."api/goldenCardSystem/consumeToAddUserPoints";        
        $level1 = $deposit['phone'];        
        $level2 = $this->findReferee($deposit['phone']);
        $allocateRatio = array(['phone'=>$deposit['phone'],'level'=>1],['phone'=>$level2['referee_mobile'],'level'=>2],['phone'=>'13000000011','level'=>3]);
        $deposit['allocateRatio'] = $allocateRatio;
        //在这里入库金卡分配情况
        $rsa =new Rsa();
        $token           = time();
        $json_data = json_encode($deposit);        
        // 公钥加密
        $rsaStr = $rsa->pubEncrypt($json_data);//post参数
        
        $data=array("Data"=>$json_data,"Token"=>$rsaStr);
        $result = http_post($url, $data);
        $resultArr = json_decode($result,true);
        return $resultArr;    
    }
	//查找服务人
    public function findReferee($mobile){
        return $referee = Db::name('member')->where('mobile', $mobile)->field( 'referee_mobile,real_name')->find();        
    }
}
